<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0,viewport-fit=cover"><title>如何在s3使用断点续传 | 刘超的博客</title><meta name="author" content="刘超"><meta name="copyright" content="刘超"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="前言   &nbsp;&nbsp;&nbsp;&nbsp;上一章节已经讲过如何基本配置一个S3上传,详情可见使用S3上传文章,接下来讲的是如何在S3中如何使用断点续传,暂停取消,继续等功能,上一章节的配置已经满足不了我的需求,所以需要使用另外的api api createMultipartUpload 创建切片  uploadPart 切片分段上传 completeMultipartUpload">
<meta property="og:type" content="article">
<meta property="og:title" content="如何在s3使用断点续传">
<meta property="og:url" content="https://leachliu.gitee.io/2022/08/15/react/%E5%A6%82%E4%BD%95%E5%9C%A8s3%E4%BD%BF%E7%94%A8%E6%96%AD%E7%82%B9%E7%BB%AD%E4%BC%A0/index.html">
<meta property="og:site_name" content="刘超的博客">
<meta property="og:description" content="前言   &nbsp;&nbsp;&nbsp;&nbsp;上一章节已经讲过如何基本配置一个S3上传,详情可见使用S3上传文章,接下来讲的是如何在S3中如何使用断点续传,暂停取消,继续等功能,上一章节的配置已经满足不了我的需求,所以需要使用另外的api api createMultipartUpload 创建切片  uploadPart 切片分段上传 completeMultipartUpload">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://leachliu.gitee.io/images/avatar.webp">
<meta property="article:published_time" content="2022-08-14T16:00:00.000Z">
<meta property="article:modified_time" content="2023-11-03T07:37:06.449Z">
<meta property="article:author" content="刘超">
<meta property="article:tag" content="S3">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://leachliu.gitee.io/images/avatar.webp"><link rel="shortcut icon" href="/images/favicon.ico"><link rel="canonical" href="https://leachliu.gitee.io/2022/08/15/react/%E5%A6%82%E4%BD%95%E5%9C%A8s3%E4%BD%BF%E7%94%A8%E6%96%AD%E7%82%B9%E7%BB%AD%E4%BC%A0/index.html"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox/fancybox.min.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = {
  root: '/',
  algolia: {"appId":"VFZO06XK8A","apiKey":"1202cec78a85d208874b3c9cbcd0e212","indexName":"index total","hits":{"per_page":10},"languages":{"input_placeholder":"搜索文章","hits_empty":"找不到您查询的内容：${query}","hits_stats":"找到 ${hits} 条结果，用时 ${time} 毫秒"}},
  localSearch: undefined,
  translate: undefined,
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '',
  dateSuffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: undefined,
  infinitegrid: {
    js: 'https://cdn.jsdelivr.net/npm/@egjs/infinitegrid/dist/infinitegrid.min.js',
    buttonText: '加载更多'
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isAnchor: false,
  percent: {
    toc: true,
    rightside: false,
  },
  autoDarkmode: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: '如何在s3使用断点续传',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2023-11-03 15:37:06'
}</script><script>(win=>{
      win.saveToLocal = {
        set: (key, value, ttl) => {
          if (ttl === 0) return
          const now = Date.now()
          const expiry = now + ttl * 86400000
          const item = {
            value,
            expiry
          }
          localStorage.setItem(key, JSON.stringify(item))
        },
      
        get: key => {
          const itemStr = localStorage.getItem(key)
      
          if (!itemStr) {
            return undefined
          }
          const item = JSON.parse(itemStr)
          const now = Date.now()
      
          if (now > item.expiry) {
            localStorage.removeItem(key)
            return undefined
          }
          return item.value
        }
      }
    
      win.getScript = (url, attr = {}) => new Promise((resolve, reject) => {
        const script = document.createElement('script')
        script.src = url
        script.async = true
        script.onerror = reject
        script.onload = script.onreadystatechange = function() {
          const loadState = this.readyState
          if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
          script.onload = script.onreadystatechange = null
          resolve()
        }

        Object.keys(attr).forEach(key => {
          script.setAttribute(key, attr[key])
        })

        document.head.appendChild(script)
      })
    
      win.getCSS = (url, id = false) => new Promise((resolve, reject) => {
        const link = document.createElement('link')
        link.rel = 'stylesheet'
        link.href = url
        if (id) link.id = id
        link.onerror = reject
        link.onload = link.onreadystatechange = function() {
          const loadState = this.readyState
          if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
          link.onload = link.onreadystatechange = null
          resolve()
        }
        document.head.appendChild(link)
      })
    
      win.activateDarkMode = () => {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = () => {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
        if (t === 'dark') activateDarkMode()
        else if (t === 'light') activateLightMode()
      
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
      const detectApple = () => {
        if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
          document.documentElement.classList.add('apple')
        }
      }
      detectApple()
    })(window)</script><link rel="stylesheet" href="https://npm.elemecdn.com/ethan4116-blog/lib/css/plane_v2.css"><link rel="stylesheet" href="https://fastly.jsdelivr.net/gh/l-lin/font-awesome-animation/dist/font-awesome-animation.min.css"  media="defer" onload="this.media='all'"><script defer src="/js/custom.js"></script><link rel="stylesheet" href="https://fastly.jsdelivr.net/gh/HCLonely/images@master/others/heartbeat.min.css"><link rel="stylesheet" href="/css/universe.css"><link rel="stylesheet" href="/css/custom.css"><!-- hexo injector head_end start --><link rel="stylesheet" href="https://npm.elemecdn.com/hexo-butterfly-tag-plugins-plus@latest/lib/assets/font-awesome-animation.min.css" media="defer" onload="this.media='all'"><link rel="stylesheet" href="https://npm.elemecdn.com/hexo-butterfly-tag-plugins-plus@latest/lib/tag_plugins.css" media="defer" onload="this.media='all'"><script src="https://npm.elemecdn.com/hexo-butterfly-tag-plugins-plus@latest/lib/assets/carousel-touch.js"></script><!-- hexo injector head_end end --><meta name="generator" content="Hexo 7.0.0"></head><body><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="/images/avatar.webp" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="sidebar-site-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">41</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">21</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">6</div></a></div><hr class="custom-hr"/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 时间轴</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="not-top-img" id="page-header"><nav id="nav"><span id="blog-info"><a href="/" title="刘超的博客"><span class="site-name">刘超的博客</span></a></span><div id="menus"><div id="search-button"><a class="site-page social-icon search" href="javascript:void(0);"><i class="fas fa-search fa-fw"></i><span> 搜索</span></a></div><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 时间轴</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div></div><div id="toggle-menu"><a class="site-page" href="javascript:void(0);"><i class="fas fa-bars fa-fw"></i></a></div></div></nav></header><main class="layout" id="content-inner"><div id="post"><div id="post-info"><h1 class="post-title">如何在s3使用断点续传</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2022-08-14T16:00:00.000Z" title="发表于 2022-08-15 00:00:00">2022-08-15</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2023-11-03T07:37:06.449Z" title="更新于 2023-11-03 15:37:06">2023-11-03</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/react/">react</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="如何在s3使用断点续传"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"><i class="fa-solid fa-spinner fa-spin"></i></span></span></div></div></div><article class="post-content" id="article-container"><h2 id="前言"><a href="#前言" class="headerlink" title="前言"></a>前言</h2><p>   &nbsp;&nbsp;&nbsp;&nbsp;上一章节已经讲过如何基本配置一个S3上传,详情可见<a href="/blog/2022/08/13/react/%E5%A6%82%E4%BD%95%E4%BD%BF%E7%94%A8s3%E4%B8%8A%E4%BC%A0%E6%96%87%E4%BB%B6/">使用S3上传文章</a>,接下来讲的是如何在S3中如何使用断点续传,暂停取消,继续等功能,上一章节的配置已经满足不了我的需求,所以需要使用另外的api</p>
<h2 id="api"><a href="#api" class="headerlink" title="api"></a>api</h2><ul>
<li>createMultipartUpload 创建切片 </li>
<li>uploadPart 切片分段上传</li>
<li>completeMultipartUpload 完成上传 </li>
<li>abortMultipartUpload 中止上传</li>
</ul>
<h2 id="创建切片"><a href="#创建切片" class="headerlink" title="创建切片"></a>创建切片</h2><h3 id="createMultipartUpload"><a href="#createMultipartUpload" class="headerlink" title="createMultipartUpload"></a>createMultipartUpload</h3><ul>
<li>Key 本次切片文件存储在S3的文件路径</li>
<li>ACL 文件读取的方式</li>
<li>Expires 对象不再可缓存的日期和时间</li>
<li>ContentType 描述内容格式   <figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line">  <span class="keyword">var</span> params = &#123;</span><br><span class="line">    <span class="title class_">Bucket</span>: <span class="string">&quot;examplebucket&quot;</span>, </span><br><span class="line">    <span class="title class_">Key</span>: <span class="string">&quot;largeobject&quot;</span>, </span><br><span class="line">    <span class="attr">ACL</span>: <span class="string">&#x27;public-read&#x27;</span>,  <span class="comment">//设置文件为公共读取</span></span><br><span class="line">    <span class="title class_">Expires</span>: <span class="keyword">new</span> <span class="title class_">Date</span>(), <span class="comment">//一般为new Date()</span></span><br><span class="line">    <span class="title class_">ContentType</span>: type <span class="comment">// 获取文件的content-type</span></span><br><span class="line">  &#125;;</span><br><span class="line">  s3.<span class="title function_">createMultipartUpload</span>(params, <span class="keyword">function</span>(<span class="params">err, data</span>) &#123;</span><br><span class="line">    <span class="keyword">if</span> (err) <span class="variable language_">console</span>.<span class="title function_">log</span>(err, err.<span class="property">stack</span>); <span class="comment">// an error occurred</span></span><br><span class="line">    <span class="keyword">else</span>     <span class="variable language_">console</span>.<span class="title function_">log</span>(data);           <span class="comment">// successful response</span></span><br><span class="line">    <span class="comment">// 包含一个uploadId </span></span><br><span class="line">    <span class="comment">/*</span></span><br><span class="line"><span class="comment">    data = &#123;</span></span><br><span class="line"><span class="comment">      Bucket: &quot;examplebucket&quot;, </span></span><br><span class="line"><span class="comment">      Key: &quot;largeobject&quot;, </span></span><br><span class="line"><span class="comment">      UploadId: &quot;ibZBv_75gd9r8lH_gqXatLdxMVpAlj6ZQjEs.OwyF3953YdwbcQnMA2BLGn8Lx12fQNICtMw5KyteFeHw.Sjng--&quot;</span></span><br><span class="line"><span class="comment">    &#125;</span></span><br><span class="line"><span class="comment">    */</span></span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure></li>
</ul>
<h2 id="切片分段上传"><a href="#切片分段上传" class="headerlink" title="切片分段上传"></a>切片分段上传</h2><h3 id="uploadPart"><a href="#uploadPart" class="headerlink" title="uploadPart"></a>uploadPart</h3><ul>
<li><p>Key 本次切片文件存储在S3的文件路径</p>
</li>
<li><p>UploadId: 上一步得到的UploadId</p>
</li>
<li><p>PartNumber: 分割切片的自序 大小限制为 1~10000,也是切片分割的份数</p>
</li>
<li><p>Body: 本次切割的数据 类型为 Buffer, Typed Array, Blob, String, ReadableStream</p>
 <figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="comment">//参数一：请求的参数</span></span><br><span class="line"><span class="comment">//参数二：请求成功的回调，err != null 代表本次失败</span></span><br><span class="line"><span class="comment">//data != null 代表本次请求成功，成功后会得到一个 Etag 数据 后面完成分段上传 需要用到</span></span><br><span class="line"><span class="variable constant_">S3</span>.<span class="title function_">uploadPart</span>( </span><br><span class="line">&#123;</span><br><span class="line">   <span class="title class_">Bucket</span>: <span class="string">&#x27;STRING_VALUE&#x27;</span>, <span class="comment">/* required */</span></span><br><span class="line">   <span class="title class_">Key</span>: <span class="string">&#x27;STRING_VALUE&#x27;</span>, <span class="comment">/* required */</span></span><br><span class="line">   <span class="title class_">PartNumber</span>: <span class="string">&#x27;NUMBER_VALUE&#x27;</span>, <span class="comment">/* required */</span></span><br><span class="line">   <span class="title class_">UploadId</span>: <span class="string">&#x27;STRING_VALUE&#x27;</span>, <span class="comment">/* required */</span></span><br><span class="line">   <span class="title class_">Body</span>:  <span class="title class_">Buffer</span>.<span class="title function_">from</span>(<span class="string">&#x27;...&#x27;</span>) || <span class="string">&#x27;STRING_VALUE&#x27;</span> || streamObject, <span class="comment">// file.slice(x, x*n) 例如从0到1</span></span><br><span class="line">&#125;,<span class="function">(<span class="params">err,data</span>)=&gt;</span>&#123;</span><br><span class="line">  <span class="variable language_">console</span>.<span class="title function_">log</span>(err)</span><br><span class="line">  <span class="keyword">if</span>(data)&#123;</span><br><span class="line">      <span class="comment">// 在结束上传时 需要用到Parts</span></span><br><span class="line">      <span class="title class_">Parts</span>.<span class="title function_">push</span>(&#123;</span><br><span class="line">        <span class="title class_">ETag</span>: data.<span class="property">ETag</span>, <span class="comment">//每一段的标记</span></span><br><span class="line">        <span class="title class_">PartNumber</span>: <span class="string">&#x27;NUMBER_VALUE&#x27;</span> <span class="comment">// 第几块数据</span></span><br><span class="line">      &#125;);</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="variable language_">console</span>.<span class="title function_">log</span>(data)</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure></li>
</ul>
<h2 id="完成上传"><a href="#完成上传" class="headerlink" title="完成上传"></a>完成上传</h2><h3 id="completeMultipartUpload"><a href="#completeMultipartUpload" class="headerlink" title="completeMultipartUpload"></a>completeMultipartUpload</h3><ul>
<li><p>Key 本次切片文件存储在S3的文件路径</p>
</li>
<li><p>UploadId 第一步得到的UploadId</p>
</li>
<li><p>MultipartUpload 第二步得到的Etag数据</p>
 <figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"> <span class="comment">//参数一：请求的参数</span></span><br><span class="line"> <span class="comment">// MultipartUpload 是一个对象</span></span><br><span class="line"> <span class="comment">//参数二：请求的回调，失败时 err != null, 成功时 data != null</span></span><br><span class="line"> <span class="variable constant_">S3</span>.<span class="title function_">completeMultipartUpload</span>(&#123;</span><br><span class="line">  <span class="title class_">Key</span>: <span class="string">&#x27;&#x27;</span>,</span><br><span class="line">  <span class="title class_">UploadId</span>: <span class="string">&#x27;&#x27;</span>,</span><br><span class="line">  <span class="title class_">MultipartUpload</span>: &#123;</span><br><span class="line">    <span class="title class_">Parts</span>: [<span class="comment">//这个数组的PartNumber必须是一个升序的排列，自小向大的升序</span></span><br><span class="line">      &#123;</span><br><span class="line">        <span class="title class_">ETag</span>: <span class="string">&#x27;&#x27;</span>,<span class="comment">//每一次切片返回的Etag数据</span></span><br><span class="line">        <span class="title class_">PartNumber</span>: <span class="string">&#x27;&#x27;</span><span class="comment">//对应每次请求的序列号</span></span><br><span class="line">      &#125;</span><br><span class="line">    ]</span><br><span class="line">  &#125;</span><br><span class="line">&#125;, <span class="keyword">function</span> (<span class="params">err, data</span>) &#123;</span><br><span class="line">    </span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure></li>
</ul>
<h2 id="中止上传"><a href="#中止上传" class="headerlink" title="中止上传"></a>中止上传</h2><h3 id="abortMultipartUpload"><a href="#abortMultipartUpload" class="headerlink" title="abortMultipartUpload"></a>abortMultipartUpload</h3><ul>
<li>Key 本次切片文件存储在S3的文件路径</li>
<li>UploadId 第一步得到的UploadId</li>
</ul>
   <figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="comment">// 如果当前正在进行任何部分上传，则这些部分上传可能会成功，也可能不会成功。因此，可能需要多次中止给定的分段上传，以完全释放所有部分消耗的所有存储空间</span></span><br><span class="line"><span class="variable constant_">S3</span>.<span class="title function_">abortMultipartUpload</span>(&#123;</span><br><span class="line">    <span class="title class_">Key</span>: required,</span><br><span class="line">    <span class="title class_">UploadId</span>: required</span><br><span class="line">  &#125;, <span class="keyword">function</span> (<span class="params"></span>) &#123;</span><br><span class="line">      </span><br><span class="line"> &#125;)</span><br></pre></td></tr></table></figure>

<h2 id="完整代码"><a href="#完整代码" class="headerlink" title="完整代码"></a>完整代码</h2>   <figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"> <span class="keyword">import</span> &#123; <span class="title class_">Button</span>, <span class="title class_">Progress</span>, message &#125; <span class="keyword">from</span> <span class="string">&#x27;antd&#x27;</span>;</span><br><span class="line"> <span class="keyword">import</span> &#123; <span class="title class_">PlusOutlined</span> &#125; <span class="keyword">from</span> <span class="string">&#x27;@ant-design/icons&#x27;</span>;</span><br><span class="line"> <span class="keyword">import</span> <span class="variable constant_">AWS</span> <span class="keyword">from</span> <span class="string">&#x27;aws-sdk&#x27;</span>;</span><br><span class="line"> <span class="keyword">import</span> &#123; getImageStyle, getVideoBase64, randomString &#125; <span class="keyword">from</span> <span class="string">&#x27;@/common/utils&#x27;</span>;</span><br><span class="line"> <span class="keyword">import</span> &#123; strName, strSuffix, trimSpecial &#125; <span class="keyword">from</span> <span class="string">&#x27;@/pages/classification/entry&#x27;</span>;</span><br><span class="line"> <span class="keyword">import</span> <span class="title class_">React</span>, &#123; useEffect, useRef, useState &#125; <span class="keyword">from</span> <span class="string">&#x27;react&#x27;</span>;</span><br><span class="line"> <span class="keyword">import</span> styles <span class="keyword">from</span> <span class="string">&#x27;../../index.module.scss&#x27;</span>;</span><br><span class="line"> <span class="keyword">import</span> <span class="title class_">SvgIcon</span> <span class="keyword">from</span> <span class="string">&#x27;@/components/SvgIcon&#x27;</span>;</span><br><span class="line"> <span class="keyword">import</span> useOnline <span class="keyword">from</span> <span class="string">&#x27;@/hooks/useOnline&#x27;</span>;</span><br><span class="line"> <span class="keyword">import</span> &#123; <span class="title class_">Subject</span> &#125; <span class="keyword">from</span> <span class="string">&#x27;rxjs&#x27;</span>;</span><br><span class="line"> type status = <span class="string">&#x27;success&#x27;</span> | <span class="string">&#x27;exception&#x27;</span> | <span class="string">&#x27;normal&#x27;</span> | <span class="string">&#x27;active&#x27;</span>;</span><br><span class="line"> <span class="comment">//配置S3的接口</span></span><br><span class="line"> <span class="keyword">export</span> interface <span class="title class_">IS3Config</span> &#123;</span><br><span class="line">   <span class="title class_">AccessKeyId</span>?: string;</span><br><span class="line">   <span class="title class_">SecrerAccessKey</span>?: string;</span><br><span class="line">   <span class="title class_">SessionToken</span>?: string;</span><br><span class="line"> &#125;</span><br><span class="line"> interface S3UploadProps &#123;</span><br><span class="line">   <span class="attr">value</span>: any;</span><br><span class="line">   closeStatus?: boolean;</span><br><span class="line">   <span class="attr">onChange</span>: <span class="function">(<span class="params">value</span>) =&gt;</span> <span class="keyword">void</span>;</span><br><span class="line"> &#125;</span><br><span class="line"> <span class="keyword">const</span> imgSetList = <span class="keyword">new</span> <span class="title class_">Set</span>([<span class="string">&#x27;image/jpg&#x27;</span>, <span class="string">&#x27;image/jpeg&#x27;</span>, <span class="string">&#x27;image/png&#x27;</span>, <span class="string">&#x27;image/webp&#x27;</span>, <span class="string">&#x27;image/svg+xml&#x27;</span>]);</span><br><span class="line"> <span class="keyword">const</span> videoSetList = <span class="keyword">new</span> <span class="title class_">Set</span>([<span class="string">&#x27;mp4&#x27;</span>, <span class="string">&#x27;video/mp4&#x27;</span>, <span class="string">&#x27;video/avi&#x27;</span>, <span class="string">&#x27;video/flv&#x27;</span>]);</span><br><span class="line"> <span class="comment">//由后端提供</span></span><br><span class="line"> <span class="keyword">const</span> s3Credentials = &#123;</span><br><span class="line">   <span class="attr">accessKeyId</span>: <span class="string">&#x27;xxxxxx&#x27;</span>,</span><br><span class="line">   <span class="attr">secretAccessKey</span>: <span class="string">&#x27;xxxxxx&#x27;</span>,</span><br><span class="line">   <span class="attr">region</span>: <span class="string">&#x27;us-west-2&#x27;</span>,</span><br><span class="line">   <span class="attr">apiVersion</span>: <span class="string">&#x27;xxxxx&#x27;</span>,</span><br><span class="line">   <span class="attr">params</span>: &#123;</span><br><span class="line">     <span class="title class_">Bucket</span>: <span class="string">&#x27;xxxxx&#x27;</span></span><br><span class="line">   &#125;</span><br><span class="line"> &#125;;</span><br><span class="line"> <span class="keyword">let</span> chunkSize = <span class="number">5</span> * <span class="number">1024</span> * <span class="number">1024</span>;</span><br><span class="line"> <span class="keyword">let</span> <span class="title class_">Parts</span>: [] = [];</span><br><span class="line"> <span class="keyword">let</span> <span class="attr">flag</span>: boolean = <span class="literal">false</span>;</span><br><span class="line"> <span class="keyword">let</span> <span class="attr">fileCount</span>: number = <span class="number">0</span>;</span><br><span class="line"> <span class="keyword">let</span> <span class="attr">uploadId</span>: string = <span class="string">&#x27;&#x27;</span>;</span><br><span class="line"> <span class="keyword">let</span> <span class="attr">offset</span>: number = <span class="number">1</span>;</span><br><span class="line"> <span class="keyword">let</span> <span class="attr">stop</span>: number = <span class="number">0</span>;</span><br><span class="line"> <span class="keyword">let</span> <span class="attr">objKey</span>: string = <span class="string">&#x27;&#x27;</span>;</span><br><span class="line"> <span class="keyword">const</span> <span class="title function_">S3UploadCom</span> = (<span class="params">props: S3UploadProps</span>) =&gt; &#123;</span><br><span class="line">   <span class="keyword">const</span> &#123; onChange, value, closeStatus &#125; = props;</span><br><span class="line">   <span class="keyword">const</span> [status, setStatus] = useState&lt;status&gt;();</span><br><span class="line">   <span class="keyword">const</span> [showDelete, setShowDelete] = <span class="title function_">useState</span>(<span class="literal">false</span>);</span><br><span class="line">   <span class="keyword">const</span> [resertStatus, setResertStatus] = <span class="title function_">useState</span>(<span class="literal">false</span>);</span><br><span class="line">   <span class="keyword">const</span> [progress, setProgress] = <span class="title function_">useState</span>(<span class="number">0</span>);</span><br><span class="line">   <span class="keyword">const</span> <span class="attr">inputRef</span>: any = <span class="title function_">useRef</span>(<span class="literal">null</span>);</span><br><span class="line">   <span class="keyword">const</span> isOnline = <span class="title function_">useOnline</span>();</span><br><span class="line">   <span class="keyword">const</span> [title, setTitle] = <span class="title function_">useState</span>(<span class="string">&#x27;&#x27;</span>);</span><br><span class="line">   <span class="keyword">const</span> <span class="attr">fileObjRef</span>: any = <span class="title function_">useRef</span>(&#123;</span><br><span class="line">     <span class="attr">file</span>: &#123;&#125;</span><br><span class="line">   &#125;);</span><br><span class="line">   <span class="keyword">const</span> <span class="title function_">isImgVideo</span> = (<span class="params">type: string</span>) =&gt; &#123;</span><br><span class="line">     <span class="keyword">if</span> (imgSetList.<span class="title function_">has</span>(type)) &#123;</span><br><span class="line">       <span class="keyword">return</span> <span class="string">&#x27;images/community/erp/&#x27;</span>;</span><br><span class="line">     &#125; <span class="keyword">else</span> <span class="keyword">if</span> (videoSetList.<span class="title function_">has</span>(type)) &#123;</span><br><span class="line">       <span class="keyword">return</span> <span class="string">&#x27;video/community/erp/&#x27;</span>;</span><br><span class="line">     &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">       <span class="keyword">return</span> <span class="string">&#x27;test/community/erp/&#x27;</span>;</span><br><span class="line">     &#125;</span><br><span class="line">   &#125;;</span><br><span class="line"></span><br><span class="line">   <span class="keyword">const</span> <span class="title function_">resertData</span> = (<span class="params"></span>) =&gt; &#123;</span><br><span class="line">     <span class="title class_">Parts</span> = [];</span><br><span class="line">     flag = <span class="literal">false</span>;</span><br><span class="line">     fileCount = <span class="number">0</span>;</span><br><span class="line">     uploadId = <span class="string">&#x27;&#x27;</span>;</span><br><span class="line">     <span class="title function_">setProgress</span>(<span class="number">0</span>);</span><br><span class="line">     offset = <span class="number">1</span>;</span><br><span class="line">     stop = <span class="number">0</span>;</span><br><span class="line">     objKey = <span class="string">&#x27;&#x27;</span>;</span><br><span class="line">     fileObjRef.<span class="property">current</span>.<span class="property">file</span> = &#123;&#125;;</span><br><span class="line">   &#125;;</span><br><span class="line"></span><br><span class="line">   <span class="keyword">const</span> formartSize = &lt;T <span class="keyword">extends</span> number&gt;(<span class="attr">size</span>: T): <span class="function"><span class="params">number</span> =&gt;</span> &#123;</span><br><span class="line">     <span class="keyword">return</span> <span class="built_in">parseFloat</span>((size / <span class="number">1000</span>).<span class="title function_">toFixed</span>(<span class="number">2</span>));</span><br><span class="line">   &#125;;</span><br><span class="line"></span><br><span class="line">   <span class="keyword">const</span> uploadUp = (<span class="attr">e</span>: &#123; <span class="attr">target</span>: &#123; <span class="attr">files</span>: any[] &#125; &#125;): <span class="function"><span class="params">void</span> =&gt;</span> &#123;</span><br><span class="line">     <span class="keyword">const</span> file = e.<span class="property">target</span>.<span class="property">files</span> &amp;&amp; e.<span class="property">target</span>.<span class="property">files</span>[<span class="number">0</span>];</span><br><span class="line">     <span class="title function_">upload</span>(file);</span><br><span class="line">   &#125;;</span><br><span class="line"></span><br><span class="line">   <span class="title function_">useEffect</span>(<span class="function">() =&gt;</span> &#123;</span><br><span class="line">     !isOnline &amp;&amp; <span class="title function_">setResertStatus</span>(<span class="literal">true</span>);</span><br><span class="line">     closeStatus &amp;&amp; <span class="title function_">gc</span>();</span><br><span class="line">     <span class="comment">// eslint-disable-next-line react-hooks/exhaustive-deps</span></span><br><span class="line">   &#125;, [isOnline, closeStatus]);</span><br><span class="line"></span><br><span class="line">   <span class="keyword">const</span> uploadTitle = (<span class="attr">file</span>: <span class="title class_">File</span>): <span class="function"><span class="params">void</span> =&gt;</span> &#123;</span><br><span class="line">     <span class="keyword">return</span> <span class="title function_">setTitle</span>(file.<span class="property">type</span>.<span class="title function_">includes</span>(<span class="string">&#x27;image&#x27;</span>) ? <span class="string">&#x27;照片&#x27;</span> : <span class="string">&#x27;视频及封面&#x27;</span>);</span><br><span class="line">   &#125;;</span><br><span class="line">   <span class="comment">// **</span></span><br><span class="line">   <span class="comment">// * 简单的计算切片的数据</span></span><br><span class="line">   <span class="comment">// * @param file</span></span><br><span class="line">   <span class="comment">// */</span></span><br><span class="line">   <span class="keyword">const</span> initFile = (file): <span class="function"><span class="params">void</span> =&gt;</span> &#123;</span><br><span class="line">     <span class="keyword">let</span> fileSize = file.<span class="property">size</span>;</span><br><span class="line">     fileCount = <span class="title class_">Math</span>.<span class="title function_">ceil</span>(fileSize / chunkSize);</span><br><span class="line">   &#125;;</span><br><span class="line">   <span class="comment">/**</span></span><br><span class="line"><span class="comment">    * 初始化得到一个S3对象</span></span><br><span class="line"><span class="comment">    * <span class="doctag">@returns</span> &#123;<span class="type">AWS.S3</span>&#125;</span></span><br><span class="line"><span class="comment">    */</span></span><br><span class="line">   <span class="keyword">const</span> init = (): <span class="variable constant_">AWS</span>.<span class="property">S3</span> =&gt; &#123;</span><br><span class="line">     <span class="keyword">return</span> <span class="keyword">new</span> <span class="variable constant_">AWS</span>.<span class="title function_">S3</span>(s3Credentials);</span><br><span class="line">   &#125;;</span><br><span class="line">   <span class="comment">/**</span></span><br><span class="line"><span class="comment">    *  对上传数组排序</span></span><br><span class="line"><span class="comment">    * <span class="doctag">@param</span> <span class="variable">arr</span></span></span><br><span class="line"><span class="comment">    * <span class="doctag">@returns</span> &#123;<span class="type">*</span>&#125;</span></span><br><span class="line"><span class="comment">    */</span></span><br><span class="line">   <span class="keyword">const</span> partSort = (<span class="attr">arr</span>: any[]): any[] =&gt; &#123;</span><br><span class="line">     <span class="keyword">if</span> (arr.<span class="property">length</span> &lt;= <span class="number">1</span>) &#123;</span><br><span class="line">       <span class="keyword">return</span> arr;</span><br><span class="line">     &#125;</span><br><span class="line">     arr.<span class="title function_">sort</span>(<span class="function">(<span class="params">objA, objB</span>) =&gt;</span> &#123;</span><br><span class="line">       <span class="keyword">var</span> n1 = objA.<span class="property">PartNumber</span>;</span><br><span class="line">       <span class="keyword">var</span> n2 = objB.<span class="property">PartNumber</span>;</span><br><span class="line"></span><br><span class="line">       <span class="keyword">if</span> (n1 &lt; n2) &#123;</span><br><span class="line">         <span class="keyword">return</span> -<span class="number">1</span>;</span><br><span class="line">       &#125; <span class="keyword">else</span> <span class="keyword">if</span> (n1 &gt; n2) &#123;</span><br><span class="line">         <span class="keyword">return</span> <span class="number">1</span>;</span><br><span class="line">       &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">         <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">       &#125;</span><br><span class="line">     &#125;);</span><br><span class="line"></span><br><span class="line">     <span class="keyword">return</span> arr;</span><br><span class="line">   &#125;;</span><br><span class="line">   <span class="comment">/**</span></span><br><span class="line"><span class="comment">    * 通知 s3 合并文件</span></span><br><span class="line"><span class="comment">    *</span></span><br><span class="line"><span class="comment">    */</span></span><br><span class="line">   <span class="keyword">const</span> mergeFile = <span class="keyword">async</span> (): <span class="title class_">Promise</span>&lt;<span class="keyword">void</span>&gt; =&gt; &#123;</span><br><span class="line">     <span class="title function_">init</span>().<span class="title function_">completeMultipartUpload</span>(</span><br><span class="line">       &#123;</span><br><span class="line">         <span class="title class_">Key</span>: objKey,</span><br><span class="line">         <span class="title class_">Bucket</span>: <span class="string">&#x27;fs-community&#x27;</span>,</span><br><span class="line">         <span class="title class_">UploadId</span>: uploadId,</span><br><span class="line">         <span class="title class_">MultipartUpload</span>: &#123;</span><br><span class="line">           <span class="title class_">Parts</span>: <span class="title function_">partSort</span>(<span class="title class_">Parts</span>)</span><br><span class="line">         &#125;</span><br><span class="line">       &#125;,</span><br><span class="line">       <span class="keyword">async</span> (err, data) =&gt; &#123;</span><br><span class="line">         <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;##completeMultipartUpload callback##&#x27;</span>);</span><br><span class="line">         <span class="keyword">if</span> (err) &#123;</span><br><span class="line">           <span class="keyword">let</span> msg = err.<span class="property">message</span>;</span><br><span class="line">           <span class="title function_">gc</span>();</span><br><span class="line">         &#125;</span><br><span class="line">         <span class="keyword">if</span> (data) &#123;</span><br><span class="line">           <span class="title function_">setStatus</span>(<span class="string">&#x27;success&#x27;</span>);</span><br><span class="line">           <span class="keyword">const</span> &#123; type, name, uid, size &#125; = fileObjRef.<span class="property">current</span>.<span class="property">file</span> <span class="keyword">as</span> any;</span><br><span class="line">           <span class="keyword">try</span> &#123;</span><br><span class="line">             <span class="comment">//成功的回调</span></span><br><span class="line">             <span class="keyword">let</span> url = <span class="string">`https://media.fs.com/<span class="subst">$&#123;data.Key&#125;</span>`</span>;</span><br><span class="line">             <span class="keyword">let</span> <span class="attr">value</span>: any;</span><br><span class="line">             <span class="keyword">if</span> (type.<span class="title function_">includes</span>(<span class="string">&#x27;video&#x27;</span>)) &#123;</span><br><span class="line">               <span class="keyword">let</span> <span class="attr">coverObj</span>: any = <span class="keyword">await</span> <span class="title function_">getVideoBase64</span>(fileObjRef.<span class="property">current</span>.<span class="property">file</span>, <span class="number">5</span>);</span><br><span class="line">               <span class="keyword">let</span> &#123; duration, coverImgUrl, coverImgKey &#125; = coverObj;</span><br><span class="line">               value = &#123;</span><br><span class="line">                 uid,</span><br><span class="line">                 <span class="attr">key</span>: data.<span class="property">Key</span>,</span><br><span class="line">                 name,</span><br><span class="line">                 type,</span><br><span class="line">                 <span class="attr">volume</span>: <span class="title function_">formartSize</span>(size),</span><br><span class="line">                 url,</span><br><span class="line">                 <span class="attr">size</span>: duration,</span><br><span class="line">                 <span class="attr">thumbUrl</span>: coverImgUrl,</span><br><span class="line">                 <span class="attr">status</span>: <span class="string">&#x27;done&#x27;</span>,</span><br><span class="line">                 <span class="attr">percent</span>: <span class="number">100</span>,</span><br><span class="line">                 <span class="attr">coverImgKey</span>: coverImgKey</span><br><span class="line">               &#125;;</span><br><span class="line">             &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">               <span class="keyword">const</span> <span class="attr">imgInfo</span>: any = <span class="keyword">await</span> <span class="title function_">getImageStyle</span>(fileObjRef.<span class="property">current</span>.<span class="property">file</span>);</span><br><span class="line">               <span class="keyword">let</span> &#123; width, height &#125; = imgInfo &amp;&amp; imgInfo;</span><br><span class="line">               value = &#123;</span><br><span class="line">                 uid,</span><br><span class="line">                 <span class="attr">key</span>: data.<span class="property">Key</span>,</span><br><span class="line">                 name,</span><br><span class="line">                 type,</span><br><span class="line">                 <span class="attr">volume</span>: <span class="title function_">formartSize</span>(size),</span><br><span class="line">                 url,</span><br><span class="line">                 <span class="attr">size</span>: width + <span class="string">&#x27;*&#x27;</span> + height,</span><br><span class="line">                 <span class="attr">thumbUrl</span>: url,</span><br><span class="line">                 <span class="attr">status</span>: <span class="string">&#x27;done&#x27;</span>,</span><br><span class="line">                 <span class="attr">percent</span>: <span class="number">100</span></span><br><span class="line">               &#125;;</span><br><span class="line">             &#125;</span><br><span class="line">             <span class="built_in">setTimeout</span>(<span class="function">() =&gt;</span> &#123;</span><br><span class="line">               onChange &amp;&amp; <span class="title function_">onChange</span>(value);</span><br><span class="line">             &#125;, <span class="number">1000</span>);</span><br><span class="line">           &#125; <span class="keyword">catch</span> (error) &#123;</span><br><span class="line">             message.<span class="title function_">error</span>(err);</span><br><span class="line">           &#125;</span><br><span class="line">         &#125;</span><br><span class="line">       &#125;</span><br><span class="line">     );</span><br><span class="line">   &#125;;</span><br><span class="line"></span><br><span class="line">   <span class="comment">/**</span></span><br><span class="line"><span class="comment">    * 具体分片上传文件到s3</span></span><br><span class="line"><span class="comment">    */</span></span><br><span class="line">   <span class="keyword">const</span> shard = (): <span class="literal">false</span> | <span class="function"><span class="params">undefined</span> =&gt;</span> &#123;</span><br><span class="line">     <span class="comment">/**</span></span><br><span class="line"><span class="comment">      * 设置手动暂停 可手动取消</span></span><br><span class="line"><span class="comment">      */</span></span><br><span class="line">     <span class="keyword">if</span> (stop === <span class="number">1</span>) &#123;</span><br><span class="line">       <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">     &#125;</span><br><span class="line">     <span class="comment">/**</span></span><br><span class="line"><span class="comment">      *</span></span><br><span class="line"><span class="comment">      * 切片偏移量大于 切片总数时 停止上传分片</span></span><br><span class="line"><span class="comment">      */</span></span><br><span class="line">     <span class="keyword">if</span> (fileCount &lt; offset) &#123;</span><br><span class="line">       <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">     &#125;</span><br><span class="line">     <span class="title function_">init</span>().<span class="title function_">uploadPart</span>(</span><br><span class="line">       &#123;</span><br><span class="line">         <span class="title class_">Key</span>: objKey,</span><br><span class="line">         <span class="title class_">UploadId</span>: uploadId,</span><br><span class="line">         <span class="title class_">Bucket</span>: <span class="string">&#x27;fs-community&#x27;</span>,</span><br><span class="line">         <span class="title class_">PartNumber</span>: offset,</span><br><span class="line">         <span class="title class_">Body</span>: fileObjRef.<span class="property">current</span>.<span class="property">file</span>.<span class="title function_">slice</span>((offset - <span class="number">1</span>) * chunkSize, offset * chunkSize)</span><br><span class="line">       &#125;,</span><br><span class="line">       <span class="function">(<span class="params">err, data</span>) =&gt;</span> &#123;</span><br><span class="line">         <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;##uploadPart callback##&#x27;</span>);</span><br><span class="line">         <span class="keyword">if</span> (err) &#123;</span><br><span class="line">           <span class="keyword">let</span> code = err.<span class="property">code</span>;</span><br><span class="line">           <span class="comment">// msg = err.message;</span></span><br><span class="line">           <span class="keyword">if</span> (code === <span class="string">&#x27;NoSuchUpload&#x27;</span>) &#123;</span><br><span class="line">           &#125;</span><br><span class="line">         &#125;</span><br><span class="line">         <span class="keyword">if</span> (data) &#123;</span><br><span class="line">           <span class="title class_">Parts</span>.<span class="title function_">push</span>(&#123;</span><br><span class="line">             <span class="comment">//@ts-ignore</span></span><br><span class="line">             <span class="title class_">ETag</span>: data.<span class="property">ETag</span>,</span><br><span class="line">             <span class="comment">//@ts-ignore</span></span><br><span class="line">             <span class="title class_">PartNumber</span>: offset</span><br><span class="line">           &#125;);</span><br><span class="line">           <span class="keyword">let</span> p = ((<span class="built_in">parseInt</span>(<span class="title class_">Parts</span>.<span class="property">length</span> <span class="keyword">as</span> any) * <span class="number">100</span>) / <span class="title class_">Math</span>.<span class="title function_">ceil</span>(fileCount)).<span class="title function_">toFixed</span>(<span class="number">0</span>);</span><br><span class="line">           <span class="comment">//@ts-ignore</span></span><br><span class="line">           <span class="keyword">if</span> (p !== <span class="number">100</span>) &#123;</span><br><span class="line">             <span class="title function_">setProgress</span>(+p);</span><br><span class="line">           &#125;</span><br><span class="line">           <span class="keyword">if</span> (<span class="title class_">Parts</span>.<span class="property">length</span> === fileCount) &#123;</span><br><span class="line">             <span class="title function_">mergeFile</span>();</span><br><span class="line">           &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">             <span class="keyword">if</span> (offset &lt;= fileCount) &#123;</span><br><span class="line">               offset++;</span><br><span class="line">               <span class="title function_">shard</span>();</span><br><span class="line">             &#125;</span><br><span class="line">           &#125;</span><br><span class="line">         &#125;</span><br><span class="line">         <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;##uploadPar callback##&#x27;</span>);</span><br><span class="line">       &#125;</span><br><span class="line">     );</span><br><span class="line">   &#125;;</span><br><span class="line">   <span class="comment">/**</span></span><br><span class="line"><span class="comment">    * 得到upload id 开始上传</span></span><br><span class="line"><span class="comment">    * <span class="doctag">@param</span> <span class="variable">file</span></span></span><br><span class="line"><span class="comment">    */</span></span><br><span class="line">   <span class="keyword">const</span> upload = (<span class="attr">file</span>: &#123; <span class="attr">name</span>: any; <span class="attr">type</span>: any; <span class="attr">size</span>: number &#125;): <span class="title class_">Promise</span>&lt;<span class="keyword">void</span>&gt; | <span class="function"><span class="params">void</span> =&gt;</span> &#123;</span><br><span class="line">     <span class="title function_">resertData</span>();</span><br><span class="line">     <span class="title function_">gc</span>();</span><br><span class="line">     <span class="keyword">const</span> &#123; name, type, size &#125; = file;</span><br><span class="line">     <span class="keyword">const</span> isLt1G = size / <span class="number">1024</span> / <span class="number">1024</span> &lt; <span class="number">1024</span>;</span><br><span class="line">     <span class="keyword">if</span> (!isLt1G &amp;&amp; videoSetList.<span class="title function_">has</span>(type)) &#123;</span><br><span class="line">       message.<span class="title function_">error</span>(<span class="string">&#x27;上传视频文件需小于1G!&#x27;</span>);</span><br><span class="line">       <span class="keyword">return</span> <span class="title class_">Promise</span>.<span class="title function_">reject</span>();</span><br><span class="line">     &#125;</span><br><span class="line">     <span class="keyword">if</span> (imgSetList.<span class="title function_">has</span>(type)) &#123;</span><br><span class="line">       <span class="keyword">const</span> isLt3M = size / <span class="number">1024</span> &lt;= <span class="number">3200</span>;</span><br><span class="line">       <span class="keyword">if</span> (!isLt3M) &#123;</span><br><span class="line">         message.<span class="title function_">error</span>(<span class="string">&#x27;上传图片文件需小于3M!&#x27;</span>);</span><br><span class="line">         <span class="keyword">return</span> <span class="title class_">Promise</span>.<span class="title function_">reject</span>();</span><br><span class="line">       &#125;</span><br><span class="line">     &#125;</span><br><span class="line">     <span class="keyword">let</span> key =</span><br><span class="line">       <span class="title function_">isImgVideo</span>(type) +</span><br><span class="line">       <span class="title function_">randomString</span>(<span class="number">5</span>) +</span><br><span class="line">       <span class="string">`_<span class="subst">$&#123;trimSpecial(strName(name))&#125;</span><span class="subst">$&#123;randomString(<span class="number">5</span>)&#125;</span>.<span class="subst">$&#123;strSuffix(name)&#125;</span>`</span>;</span><br><span class="line">     fileObjRef.<span class="property">current</span>.<span class="property">file</span> = file;</span><br><span class="line">     <span class="title function_">uploadTitle</span>(file <span class="keyword">as</span> any);</span><br><span class="line">     objKey = key;</span><br><span class="line">     <span class="title function_">setProgress</span>(<span class="number">1</span>);</span><br><span class="line">     <span class="keyword">var</span> params = &#123;</span><br><span class="line">       <span class="title class_">Key</span>: objKey,</span><br><span class="line">       <span class="attr">ACL</span>: <span class="string">&#x27;public-read&#x27;</span>, <span class="comment">// 默认情况下，文件是私有的。我希望这些文件公开阅读。</span></span><br><span class="line">       <span class="title class_">Bucket</span>: <span class="string">&#x27;fs-community&#x27;</span>,</span><br><span class="line">       <span class="title class_">Expires</span>: <span class="keyword">new</span> <span class="title class_">Date</span>(),</span><br><span class="line">       <span class="title class_">ContentType</span>: type</span><br><span class="line">     &#125;;</span><br><span class="line"></span><br><span class="line">     <span class="keyword">if</span> (stop === <span class="number">1</span>) &#123;</span><br><span class="line">       stop = <span class="number">0</span>;</span><br><span class="line">     &#125;</span><br><span class="line">     <span class="title function_">initFile</span>(file);</span><br><span class="line">     <span class="keyword">var</span> res = <span class="title function_">init</span>().<span class="title function_">createMultipartUpload</span>(params, <span class="keyword">async</span> (err, data) =&gt; &#123;</span><br><span class="line">       <span class="keyword">if</span> (err) &#123;</span><br><span class="line">       &#125;</span><br><span class="line">       <span class="keyword">if</span> (data) &#123;</span><br><span class="line">         <span class="comment">//@ts-ignore</span></span><br><span class="line">         uploadId = res.<span class="property">response</span>.<span class="property">data</span>.<span class="property">UploadId</span>;</span><br><span class="line">         <span class="title function_">shard</span>();</span><br><span class="line">       &#125;</span><br><span class="line">       <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;##createMultipartUpload callback##&#x27;</span>);</span><br><span class="line">     &#125;);</span><br><span class="line">   &#125;;</span><br><span class="line"></span><br><span class="line">   <span class="keyword">const</span> resertBegin = <span class="keyword">async</span> (): <span class="title class_">Promise</span>&lt;<span class="keyword">void</span>&gt; =&gt; &#123;</span><br><span class="line">     <span class="keyword">if</span> (flag || !isOnline) &#123;</span><br><span class="line">       <span class="keyword">return</span>;</span><br><span class="line">     &#125;</span><br><span class="line">     <span class="title function_">initFile</span>(fileObjRef.<span class="property">current</span>.<span class="property">file</span>);</span><br><span class="line">     flag = <span class="literal">true</span>;</span><br><span class="line">     <span class="title function_">setResertStatus</span>(<span class="literal">false</span>);</span><br><span class="line">     <span class="title function_">shard</span>();</span><br><span class="line">   &#125;;</span><br><span class="line"></span><br><span class="line">   <span class="comment">/**</span></span><br><span class="line"><span class="comment">    * 清理分片上传失败后的孤儿碎片</span></span><br><span class="line"><span class="comment">    */</span></span><br><span class="line">   <span class="keyword">const</span> gc = (): <span class="function"><span class="params">void</span> =&gt;</span> &#123;</span><br><span class="line">     <span class="keyword">if</span> (objKey &amp;&amp; uploadId) &#123;</span><br><span class="line">       <span class="title function_">init</span>().<span class="title function_">abortMultipartUpload</span>(</span><br><span class="line">         &#123;</span><br><span class="line">           <span class="title class_">Key</span>: objKey,</span><br><span class="line">           <span class="title class_">Bucket</span>: <span class="string">&#x27;fs-community&#x27;</span>,</span><br><span class="line">           <span class="title class_">UploadId</span>: uploadId</span><br><span class="line">         &#125;,</span><br><span class="line">         <span class="function">(<span class="params">err, data</span>) =&gt;</span> &#123;</span><br><span class="line">           <span class="keyword">if</span> (data) &#123;</span><br><span class="line">             <span class="variable language_">console</span>.<span class="title function_">log</span>(data);</span><br><span class="line">           &#125;</span><br><span class="line">           <span class="keyword">if</span> (err) &#123;</span><br><span class="line">             <span class="variable language_">console</span>.<span class="title function_">log</span>(err);</span><br><span class="line">           &#125;</span><br><span class="line">         &#125;</span><br><span class="line">       );</span><br><span class="line">     &#125;</span><br><span class="line">   &#125;;</span><br><span class="line">    </span><br><span class="line">   <span class="comment">//s3上传的api </span></span><br><span class="line">   <span class="keyword">const</span> <span class="title class_">UploadFile</span> = (<span class="attr">bucket</span>: string, <span class="attr">key</span>: string, <span class="attr">body</span>: any): <span class="title class_">Subject</span>&lt;unknown&gt; =&gt; &#123;</span><br><span class="line">     <span class="keyword">const</span> s3subject = <span class="keyword">new</span> <span class="title class_">Subject</span>(); <span class="comment">//创建一个Subject主体</span></span><br><span class="line">     <span class="title function_">init</span>()</span><br><span class="line">       .<span class="title function_">upload</span>(</span><br><span class="line">         &#123;</span><br><span class="line">           <span class="title class_">Body</span>: body, <span class="comment">// 是文件类型</span></span><br><span class="line">           <span class="title class_">Bucket</span>: bucket, <span class="comment">// 对应S3上的bucket</span></span><br><span class="line">           <span class="title class_">Key</span>: key, <span class="comment">//文件名</span></span><br><span class="line">           <span class="title class_">ContentType</span>: body.<span class="property">type</span></span><br><span class="line">         &#125;,</span><br><span class="line">         &#123;</span><br><span class="line">           <span class="attr">queueSize</span>: <span class="number">1</span>,</span><br><span class="line">           <span class="attr">partSize</span>: <span class="number">10</span> * <span class="number">1024</span> * <span class="number">1024</span></span><br><span class="line">         &#125;,</span><br><span class="line">         <span class="function">(<span class="params">err: any, resp: any</span>) =&gt;</span> &#123;</span><br><span class="line">           <span class="keyword">if</span> (err) &#123;</span><br><span class="line">             s3subject.<span class="title function_">error</span>(err); <span class="comment">// 上传失败时调用</span></span><br><span class="line">           &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">             s3subject.<span class="title function_">next</span>(resp); <span class="comment">// 上传成功时调用 如果要调用下面的subscribe方法 必须使用s3subject.next()</span></span><br><span class="line">           &#125;</span><br><span class="line">         &#125;</span><br><span class="line">       )</span><br><span class="line">       .<span class="title function_">on</span>(<span class="string">&#x27;httpUploadProgress&#x27;</span>, <span class="function"><span class="params">progress</span> =&gt;</span> &#123;</span><br><span class="line">         <span class="keyword">let</span> per = <span class="title class_">Math</span>.<span class="title function_">round</span>((progress.<span class="property">loaded</span> / progress.<span class="property">total</span>) * <span class="number">10000</span>) / <span class="number">100</span>; <span class="comment">//百分比，保留小数点两位</span></span><br><span class="line">         <span class="title function_">setProgress</span>(+per.<span class="title function_">toFixed</span>(<span class="number">0</span>));</span><br><span class="line">         per === <span class="number">100</span> &amp;&amp; <span class="title function_">setStatus</span>(<span class="string">&#x27;success&#x27;</span>);</span><br><span class="line">         body.<span class="property">onProgress</span> ? body.<span class="title function_">onProgress</span>(&#123; per &#125;, body.<span class="property">file</span>) : <span class="keyword">void</span> <span class="number">0</span>;</span><br><span class="line">       &#125;);</span><br><span class="line">     <span class="keyword">return</span> s3subject;</span><br><span class="line">   &#125;;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">   <span class="comment">//图片上传 因为图片一般格式较小 所以不需要分段上传</span></span><br><span class="line">   <span class="keyword">const</span> imagUpload = (<span class="attr">key</span>: string, <span class="attr">file</span>: any): <span class="function"><span class="params">void</span> =&gt;</span> &#123;</span><br><span class="line">     <span class="keyword">try</span> &#123;</span><br><span class="line">       <span class="keyword">const</span> &#123; type, name, size, uid &#125; = file;</span><br><span class="line">       <span class="keyword">let</span> bucket = <span class="string">&#x27;xxxxxxx&#x27;</span>;</span><br><span class="line">       <span class="keyword">let</span> <span class="attr">value</span>: any;</span><br><span class="line">       <span class="title class_">UploadFile</span>(bucket, key, file).<span class="title function_">subscribe</span>(&#123;</span><br><span class="line">         <span class="attr">next</span>: <span class="keyword">async</span> () =&gt; &#123;</span><br><span class="line">           <span class="comment">//成功的回调</span></span><br><span class="line">           <span class="keyword">let</span> url = <span class="string">`xxxxxxxx`</span>;</span><br><span class="line">           <span class="keyword">if</span> (type.<span class="title function_">includes</span>(<span class="string">&#x27;video&#x27;</span>)) &#123;</span><br><span class="line">             <span class="keyword">let</span> <span class="attr">coverObj</span>: any = <span class="keyword">await</span> <span class="title function_">getVideoBase64</span>(file, <span class="number">5</span>);</span><br><span class="line">             <span class="keyword">let</span> &#123; duration, coverImgUrl, coverImgKey &#125; = coverObj;</span><br><span class="line">             value = &#123;</span><br><span class="line">               uid,</span><br><span class="line">               key,</span><br><span class="line">               name,</span><br><span class="line">               type,</span><br><span class="line">               <span class="attr">volume</span>: <span class="title function_">formartSize</span>(size),</span><br><span class="line">               url,</span><br><span class="line">               <span class="attr">size</span>: duration,</span><br><span class="line">               <span class="attr">thumbUrl</span>: coverImgUrl,</span><br><span class="line">               <span class="attr">status</span>: <span class="string">&#x27;done&#x27;</span>,</span><br><span class="line">               <span class="attr">percent</span>: <span class="number">100</span>,</span><br><span class="line">               <span class="attr">coverImgKey</span>: coverImgKey</span><br><span class="line">             &#125;;</span><br><span class="line">           &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">             <span class="keyword">const</span> <span class="attr">imgInfo</span>: any = <span class="keyword">await</span> <span class="title function_">getImageStyle</span>(file);</span><br><span class="line">             <span class="keyword">let</span> &#123; width, height &#125; = imgInfo &amp;&amp; imgInfo;</span><br><span class="line">             value = &#123;</span><br><span class="line">               uid,</span><br><span class="line">               key,</span><br><span class="line">               name,</span><br><span class="line">               type,</span><br><span class="line">               <span class="attr">volume</span>: <span class="title function_">formartSize</span>(size),</span><br><span class="line">               url,</span><br><span class="line">               <span class="attr">size</span>: width + <span class="string">&#x27;*&#x27;</span> + height,</span><br><span class="line">               <span class="attr">thumbUrl</span>: url,</span><br><span class="line">               <span class="attr">status</span>: <span class="string">&#x27;done&#x27;</span>,</span><br><span class="line">               <span class="attr">percent</span>: <span class="number">100</span></span><br><span class="line">             &#125;;</span><br><span class="line">           &#125;</span><br><span class="line">           onChange &amp;&amp; <span class="title function_">onChange</span>(value);</span><br><span class="line">         &#125;,</span><br><span class="line">         <span class="attr">error</span>: <span class="function"><span class="params">error</span> =&gt;</span> &#123;</span><br><span class="line">           message.<span class="title function_">error</span>(error);</span><br><span class="line">         &#125;</span><br><span class="line">       &#125;);</span><br><span class="line">     &#125; <span class="keyword">catch</span> (error) &#123;</span><br><span class="line">       <span class="variable language_">console</span>.<span class="title function_">log</span>(error);</span><br><span class="line">     &#125;</span><br><span class="line">   &#125;;</span><br><span class="line"></span><br><span class="line">   <span class="keyword">const</span> <span class="title function_">resetStatus</span> = (<span class="params"></span>) =&gt; &#123;</span><br><span class="line">     onChange &amp;&amp; <span class="title function_">onChange</span>(<span class="string">&#x27;&#x27;</span>);</span><br><span class="line">     <span class="title function_">setProgress</span>(<span class="number">0</span>);</span><br><span class="line">     <span class="title function_">setStatus</span>(<span class="string">&#x27;normal&#x27;</span>);</span><br><span class="line">     <span class="title function_">setShowDelete</span>(<span class="literal">false</span>);</span><br><span class="line">   &#125;;</span><br><span class="line"></span><br><span class="line">   <span class="keyword">return</span> (</span><br><span class="line">     <span class="language-xml"><span class="tag">&lt;<span class="name">div</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">       &#123;value ? (</span></span><br><span class="line"><span class="language-xml">         <span class="tag">&lt;<span class="name">div</span> <span class="attr">className</span>=<span class="string">&#123;styles.customImgStyle_content&#125;</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">           <span class="tag">&lt;<span class="name">div</span></span></span></span><br><span class="line"><span class="tag"><span class="language-xml">             <span class="attr">className</span>=<span class="string">&#123;styles.customImgStyle_container&#125;</span></span></span></span><br><span class="line"><span class="tag"><span class="language-xml">             <span class="attr">onMouseEnter</span>=<span class="string">&#123;()</span> =&gt;</span> &#123;</span></span><br><span class="line"><span class="language-xml">               setShowDelete(true);</span></span><br><span class="line"><span class="language-xml">             &#125;&#125;</span></span><br><span class="line"><span class="language-xml">             onMouseLeave=&#123;() =&gt; &#123;</span></span><br><span class="line"><span class="language-xml">               setShowDelete(false);</span></span><br><span class="line"><span class="language-xml">             &#125;&#125;&gt;</span></span><br><span class="line"><span class="language-xml">             <span class="tag">&lt;<span class="name">div</span> <span class="attr">className</span>=<span class="string">&#123;styles.customImgStyle_box&#125;</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">               <span class="tag">&lt;<span class="name">img</span> <span class="attr">src</span>=<span class="string">&#123;value&#125;</span> <span class="attr">alt</span>=<span class="string">&quot;&quot;</span> /&gt;</span></span></span><br><span class="line"><span class="language-xml">               <span class="tag">&lt;<span class="name">div</span> <span class="attr">className</span>=<span class="string">&#123;styles.customImgStyle_name&#125;</span>&gt;</span><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">             <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">             &#123;showDelete &amp;&amp; (</span></span><br><span class="line"><span class="language-xml">               <span class="tag">&lt;<span class="name">div</span> <span class="attr">className</span>=<span class="string">&#123;styles.customImgStyle_info&#125;</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">                 <span class="tag">&lt;<span class="name">div</span></span></span></span><br><span class="line"><span class="tag"><span class="language-xml">                   <span class="attr">className</span>=<span class="string">&#123;styles.customImgStyle_info_icon&#125;</span></span></span></span><br><span class="line"><span class="tag"><span class="language-xml">                   <span class="attr">onClick</span>=<span class="string">&#123;()</span> =&gt;</span> &#123;</span></span><br><span class="line"><span class="language-xml">                     setShowDelete(false);</span></span><br><span class="line"><span class="language-xml">                     resertData();</span></span><br><span class="line"><span class="language-xml">                     resetStatus();</span></span><br><span class="line"><span class="language-xml">                   &#125;&#125;&gt;</span></span><br><span class="line"><span class="language-xml">                   <span class="tag">&lt;<span class="name">SvgIcon</span> <span class="attr">iconClass</span>=<span class="string">&#123;</span>&#x27;<span class="attr">delete</span>&#x27;&#125; <span class="attr">className</span>=<span class="string">&#123;styles.customImgStyle_info_svg&#125;</span> /&gt;</span></span></span><br><span class="line"><span class="language-xml">                 <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">               <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">             )&#125;</span></span><br><span class="line"><span class="language-xml">           <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">         <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">       ) : (</span></span><br><span class="line"><span class="language-xml">         <span class="tag">&lt;&gt;</span></span></span><br><span class="line"><span class="language-xml">           &#123;progress &gt; 0 ? (</span></span><br><span class="line"><span class="language-xml">             <span class="tag">&lt;<span class="name">div</span> <span class="attr">className</span>=<span class="string">&#123;styles.progress&#125;</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">               &#123;status === &#x27;exception&#x27; ? (</span></span><br><span class="line"><span class="language-xml">                 <span class="tag">&lt;<span class="name">div</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">                   <span class="tag">&lt;<span class="name">div</span>&gt;</span>上传失败<span class="tag">&lt;/<span class="name">div</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">                 <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">               ) : status === &#x27;success&#x27; ? (</span></span><br><span class="line"><span class="language-xml">                 <span class="tag">&lt;<span class="name">div</span>&gt;</span>上传成功<span class="tag">&lt;/<span class="name">div</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">               ) : (</span></span><br><span class="line"><span class="language-xml">                 <span class="tag">&lt;<span class="name">div</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">                   &#123;!isOnline || resertStatus ? (</span></span><br><span class="line"><span class="language-xml">                     <span class="tag">&lt;<span class="name">div</span> <span class="attr">className</span>=<span class="string">&#123;styles.content&#125;</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">                       <span class="tag">&lt;<span class="name">div</span>&gt;</span>网络错误，<span class="tag">&lt;/<span class="name">div</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">                       <span class="tag">&lt;<span class="name">div</span></span></span></span><br><span class="line"><span class="tag"><span class="language-xml">                         <span class="attr">className</span>=<span class="string">&#123;styles.content_error&#125;</span></span></span></span><br><span class="line"><span class="tag"><span class="language-xml">                         <span class="attr">onClick</span>=<span class="string">&#123;()</span> =&gt;</span> &#123;</span></span><br><span class="line"><span class="language-xml">                           resertBegin();</span></span><br><span class="line"><span class="language-xml">                         &#125;&#125;&gt;</span></span><br><span class="line"><span class="language-xml">                         点击续传</span></span><br><span class="line"><span class="language-xml">                       <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">                     <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">                   ) : (</span></span><br><span class="line"><span class="language-xml">                     <span class="tag">&lt;<span class="name">div</span>&gt;</span>正在上传&#123;title&#125;,请稍后...<span class="tag">&lt;/<span class="name">div</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">                   )&#125;</span></span><br><span class="line"><span class="language-xml">                   &#123;/* <span class="tag">&lt;<span class="name">Button</span> <span class="attr">onClick</span>=<span class="string">&#123;abortUpload&#125;</span> <span class="attr">danger</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">                     取消上传</span></span><br><span class="line"><span class="language-xml">                   <span class="tag">&lt;/<span class="name">Button</span>&gt;</span> */&#125;</span></span><br><span class="line"><span class="language-xml">                 <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">               )&#125;</span></span><br><span class="line"><span class="language-xml">               <span class="tag">&lt;<span class="name">Progress</span> <span class="attr">percent</span>=<span class="string">&#123;progress&#125;</span> <span class="attr">className</span>=<span class="string">&#123;styles.progress_percent&#125;</span> <span class="attr">status</span>=<span class="string">&#123;status&#125;</span> /&gt;</span></span></span><br><span class="line"><span class="language-xml">             <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">           ) : (</span></span><br><span class="line"><span class="language-xml">             <span class="tag">&lt;&gt;</span></span></span><br><span class="line"><span class="language-xml">               <span class="tag">&lt;<span class="name">input</span></span></span></span><br><span class="line"><span class="tag"><span class="language-xml">                 <span class="attr">type</span>=<span class="string">&quot;file&quot;</span></span></span></span><br><span class="line"><span class="tag"><span class="language-xml">                 <span class="attr">name</span>=<span class="string">&quot;image&quot;</span></span></span></span><br><span class="line"><span class="tag"><span class="language-xml">                 <span class="attr">id</span>=<span class="string">&quot;file&quot;</span></span></span></span><br><span class="line"><span class="tag"><span class="language-xml">                 <span class="attr">onChange</span>=<span class="string">&#123;uploadUp&#125;</span></span></span></span><br><span class="line"><span class="tag"><span class="language-xml">                 <span class="attr">className</span>=<span class="string">&#123;styles.input&#125;</span></span></span></span><br><span class="line"><span class="tag"><span class="language-xml">                 <span class="attr">ref</span>=<span class="string">&#123;inputRef&#125;</span></span></span></span><br><span class="line"><span class="tag"><span class="language-xml">               /&gt;</span></span></span><br><span class="line"><span class="language-xml">               <span class="tag">&lt;<span class="name">div</span> <span class="attr">className</span>=<span class="string">&#123;styles.itemRender&#125;</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">                 <span class="tag">&lt;<span class="name">PlusOutlined</span> /&gt;</span></span></span><br><span class="line"><span class="language-xml">                 <span class="tag">&lt;<span class="name">div</span> <span class="attr">className</span>=<span class="string">&#123;styles.itemRender_text&#125;</span>&gt;</span>拖拽到此处新建图片/视频<span class="tag">&lt;/<span class="name">div</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">                 <span class="tag">&lt;<span class="name">Button</span></span></span></span><br><span class="line"><span class="tag"><span class="language-xml">                   <span class="attr">type</span>=<span class="string">&quot;primary&quot;</span></span></span></span><br><span class="line"><span class="tag"><span class="language-xml">                   <span class="attr">onClick</span>=<span class="string">&#123;()</span> =&gt;</span> &#123;</span></span><br><span class="line"><span class="language-xml">                     document.getElementById(&#x27;file&#x27;)?.click();</span></span><br><span class="line"><span class="language-xml">                   &#125;&#125;&gt;</span></span><br><span class="line"><span class="language-xml">                   选择文件</span></span><br><span class="line"><span class="language-xml">                 <span class="tag">&lt;/<span class="name">Button</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">                 <span class="tag">&lt;<span class="name">div</span> <span class="attr">className</span>=<span class="string">&#123;styles.itemRender_limit&#125;</span>&gt;</span>图片不超过3M;视频不超过1GB<span class="tag">&lt;/<span class="name">div</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">               <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span></span><br><span class="line"><span class="language-xml">             <span class="tag">&lt;/&gt;</span></span></span><br><span class="line">           )&#125;</span><br><span class="line">         &lt;/&gt;</span><br><span class="line">       )&#125;</span><br><span class="line">     &lt;/div&gt;</span><br><span class="line">   );</span><br><span class="line"> &#125;</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> S3UploadCom;</span><br></pre></td></tr></table></figure></article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta"><i class="fas fa-circle-user fa-fw"></i>文章作者: </span><span class="post-copyright-info"><a href="https://leachliu.gitee.io">刘超</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta"><i class="fas fa-square-arrow-up-right fa-fw"></i>文章链接: </span><span class="post-copyright-info"><a href="https://leachliu.gitee.io/2022/08/15/react/%E5%A6%82%E4%BD%95%E5%9C%A8s3%E4%BD%BF%E7%94%A8%E6%96%AD%E7%82%B9%E7%BB%AD%E4%BC%A0/">https://leachliu.gitee.io/2022/08/15/react/%E5%A6%82%E4%BD%95%E5%9C%A8s3%E4%BD%BF%E7%94%A8%E6%96%AD%E7%82%B9%E7%BB%AD%E4%BC%A0/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta"><i class="fas fa-circle-exclamation fa-fw"></i>版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="https://leachliu.gitee.io" target="_blank">刘超的博客</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/S3/">S3</a></div><div class="post_share"><div class="social-share" data-image="/images/avatar.webp" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/js/social-share.min.js" defer></script></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/2022/08/16/js/%E6%B1%82%E5%87%BA%E6%95%B0%E7%BB%84%E4%B8%AD%E7%9A%84%E6%9C%80%E5%A4%A7%E5%80%BC/" title="求出数组中最大的值"><div class="cover" style="background: var(--default-bg-color)"></div><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">求出数组中最大的值</div></div></a></div><div class="next-post pull-right"><a href="/2022/08/13/react/%E5%A6%82%E4%BD%95%E4%BD%BF%E7%94%A8s3%E4%B8%8A%E4%BC%A0%E6%96%87%E4%BB%B6/" title="如何使用s3上传文件"><div class="cover" style="background: var(--default-bg-color)"></div><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">如何使用s3上传文件</div></div></a></div></nav><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span>相关推荐</span></div><div class="relatedPosts-list"><div><a href="/2022/08/13/react/%E5%A6%82%E4%BD%95%E4%BD%BF%E7%94%A8s3%E4%B8%8A%E4%BC%A0%E6%96%87%E4%BB%B6/" title="如何使用s3上传文件"><div class="cover" style="background: var(--default-bg-color)"></div><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-08-13</div><div class="title">如何使用s3上传文件</div></div></a></div></div></div></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src="/images/avatar.webp" onerror="this.onerror=null;this.src='/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">刘超</div><div class="author-info__description">只有自己强大，才不会被别人践踏。</div></div><div class="card-info-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">41</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">21</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">6</div></a></div><a id="card-info-btn" target="_blank" rel="noopener" href="https://github.com/xxxxxx"><i class="fab fa-github"></i><span>Follow Me</span></a><div class="card-info-social-icons is-center"><a class="social-icon" href="http://wpa.qq.com/msgrd?v=3&amp;uin=923946406&amp;site=qq&amp;menu=yes" target="_blank" title="QQ"><i class="fab fa-qq"></i></a><a class="social-icon" href="https://chao-picture-bed.oss-cn-hangzhou.aliyuncs.com/blog/vue/popperJs/202301131417748.jpg" target="_blank" title="Wechat"><i class="fab fa-weixin"></i></a><a class="social-icon" href="/923946406@qq.com" target="_blank" title="Email"><i class="fas fa-envelope"></i></a><a class="social-icon" href="https://github.com/tzy13755126023" target="_blank" title="Github"><i class="fab fa-github"></i></a><a class="social-icon" href="/atom.xml" target="_blank" title="Rss"><i class="fas fa-rss"></i></a></div></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn fa-shake"></i><span>公告</span></div><div class="announcement_content">书山有路勤为径,学海无涯苦作舟</div></div><div class="card-widget tzy-right-widget" id="card-wechat"><div id="flip-wrapper"><div id="flip-content"><div class="face"></div><div class="back face"></div></div></div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span><span class="toc-percentage"></span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%89%8D%E8%A8%80"><span class="toc-number">1.</span> <span class="toc-text">前言</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#api"><span class="toc-number">2.</span> <span class="toc-text">api</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%88%9B%E5%BB%BA%E5%88%87%E7%89%87"><span class="toc-number">3.</span> <span class="toc-text">创建切片</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#createMultipartUpload"><span class="toc-number">3.1.</span> <span class="toc-text">createMultipartUpload</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%88%87%E7%89%87%E5%88%86%E6%AE%B5%E4%B8%8A%E4%BC%A0"><span class="toc-number">4.</span> <span class="toc-text">切片分段上传</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#uploadPart"><span class="toc-number">4.1.</span> <span class="toc-text">uploadPart</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%AE%8C%E6%88%90%E4%B8%8A%E4%BC%A0"><span class="toc-number">5.</span> <span class="toc-text">完成上传</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#completeMultipartUpload"><span class="toc-number">5.1.</span> <span class="toc-text">completeMultipartUpload</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%B8%AD%E6%AD%A2%E4%B8%8A%E4%BC%A0"><span class="toc-number">6.</span> <span class="toc-text">中止上传</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#abortMultipartUpload"><span class="toc-number">6.1.</span> <span class="toc-text">abortMultipartUpload</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%AE%8C%E6%95%B4%E4%BB%A3%E7%A0%81"><span class="toc-number">7.</span> <span class="toc-text">完整代码</span></a></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/2023/11/06/comlibary/4.%E7%BB%84%E4%BB%B6%E5%BA%93%E7%B1%BB%E5%90%8D%E6%96%B9%E6%B3%95%E5%AE%9E%E7%8E%B0%E4%BB%A5%E5%8F%8A%E8%A7%84/" title="无题">无题</a><time datetime="2023-11-06T07:16:00.298Z" title="发表于 2023-11-06 15:16:00">2023-11-06</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2023/11/06/comlibary/3.%E6%90%AD%E5%BB%BA%E7%BB%84%E4%BB%B6%E5%BA%93%E5%BC%80%E5%8F%91%E7%8E%AF%E5%A2%83/" title="组件库框架 - 3 搭建组件库开发环境"><img src="/images/beijing4.webp" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="组件库框架 - 3 搭建组件库开发环境"/></a><div class="content"><a class="title" href="/2023/11/06/comlibary/3.%E6%90%AD%E5%BB%BA%E7%BB%84%E4%BB%B6%E5%BA%93%E5%BC%80%E5%8F%91%E7%8E%AF%E5%A2%83/" title="组件库框架 - 3 搭建组件库开发环境">组件库框架 - 3 搭建组件库开发环境</a><time datetime="2023-11-06T06:30:00.000Z" title="发表于 2023-11-06 14:30:00">2023-11-06</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2023/11/06/comlibary/2.%E5%88%9D%E5%A7%8B%E5%8C%96workspace-root/" title="组件库框架 - 2 初始化workspace-root"><img src="/images/beijing4.webp" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="组件库框架 - 2 初始化workspace-root"/></a><div class="content"><a class="title" href="/2023/11/06/comlibary/2.%E5%88%9D%E5%A7%8B%E5%8C%96workspace-root/" title="组件库框架 - 2 初始化workspace-root">组件库框架 - 2 初始化workspace-root</a><time datetime="2023-11-06T03:18:00.000Z" title="发表于 2023-11-06 11:18:00">2023-11-06</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2023/10/27/comlibary/1.%E6%90%AD%E5%BB%BApnpm%E3%80%81monorepo/" title="组件库框架 - 1 搭建pnpn monorepo"><img src="/images/beijing3.webp" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="组件库框架 - 1 搭建pnpn monorepo"/></a><div class="content"><a class="title" href="/2023/10/27/comlibary/1.%E6%90%AD%E5%BB%BApnpm%E3%80%81monorepo/" title="组件库框架 - 1 搭建pnpn monorepo">组件库框架 - 1 搭建pnpn monorepo</a><time datetime="2023-10-27T06:36:00.000Z" title="发表于 2023-10-27 14:36:00">2023-10-27</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2023/01/12/other/%E4%BD%BF%E7%94%A8OSS+PicGo%E6%90%AD%E5%BB%BA%E5%9B%BE%E5%BA%8A/" title="使用OSS+PicGo搭建图床"><img src="https://chao-picture-bed.oss-cn-hangzhou.aliyuncs.com/blog/vue/popperJs/202301121557184.png" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="使用OSS+PicGo搭建图床"/></a><div class="content"><a class="title" href="/2023/01/12/other/%E4%BD%BF%E7%94%A8OSS+PicGo%E6%90%AD%E5%BB%BA%E5%9B%BE%E5%BA%8A/" title="使用OSS+PicGo搭建图床">使用OSS+PicGo搭建图床</a><time datetime="2023-01-12T07:39:00.000Z" title="发表于 2023-01-12 15:39:00">2023-01-12</time></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="copyright">&copy;2020 - 2023  <i id="heartbeat" class="fa fas fa-heartbeat"></i> 刘超</div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside-config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="回到顶部"><span class="scroll-percent"></span><i class="fas fa-arrow-up"></i></button></div></div><div><script src="/js/utils.js"></script><script src="/js/main.js"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox/fancybox.umd.min.js"></script><div class="js-pjax"></div><canvas id="universe"></canvas><script defer src="/js/universe.js"></script><script defer src="https://fastly.jsdelivr.net/npm/hexo-theme-volantis@latest/source/js/issues.min.js"></script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script><div id="algolia-search"><div class="search-dialog"><nav class="search-nav"><span class="search-dialog-title">搜索</span><button class="search-close-button"><i class="fas fa-times"></i></button></nav><div class="search-wrap"><div id="algolia-search-input"></div><hr/><div id="algolia-search-results"><div id="algolia-hits"></div><div id="algolia-pagination"></div><div id="algolia-info"><div class="algolia-stats"></div><div class="algolia-poweredBy"></div></div></div></div></div><div id="search-mask"></div><script src="https://cdn.jsdelivr.net/npm/algoliasearch/dist/algoliasearch-lite.umd.min.js"></script><script src="https://cdn.jsdelivr.net/npm/instantsearch.js/dist/instantsearch.production.min.js"></script><script src="/js/search/algolia.js"></script></div></div><!-- hexo injector body_end start --><script async src="//at.alicdn.com/t/font_2032782_8d5kxvn09md.js"></script><!-- hexo injector body_end end --></body></html>